<style scoped>
._hide{opacity: 0;}
._input1{width:150px;height: 24px;}
.input_cs{position: absolute;bottom:0px;width:80%;right:10%;}
.input_cs input{text-align: right;border:0px;background:none;}
</style>
<template>
<div>
     <Modal v-model="modal" :title="title" :styles="{width:'1000px',top:'20px'}" :mask-closable="false">
        
        <div class="div_box1" id="div_box1">
            <Row :gutter="20">
                <Col span="8" style="text-align:center;font-size:12px;line-height:30px;">
                    发票代码    <input class="_input1" v-model="obj.invoiceCode" ref="invoiceCode" tabIndex="1">
                </Col>
                <Col span="6">
                    <Select    style="width:200px" v-model="obj.invoiceTypeCode" 
                    placeholder="选择发票类型" tabIndex="2">
                        <Option  value="1" >增值税普通发票</Option>
                    </Select>
                </Col>
                <Col span="8" style="text-align:center;font-size:12px;line-height:30px;">
                  发票号码 NO  
                  <input class="_input1" v-model="obj.invoiceNo" tabIndex="3"  ref="invoiceNo">

                </Col>
            </Row>
            <p style="text-align:left;font-size:12px;padding-top:15px;padding-bottom:5px;">
                
                <span style="float:left;">校验码  
                    <input class="_input1" v-model="obj.invoiceCheckCode" ref="invoiceCheckCode" tabIndex="4">
                </span>
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;查询总金额  
                    <input class="_input1" v-model="obj.totalAmount" ref="totalAmount" tabIndex="4">
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;开票日期  
                    <DatePicker type="date" :value="date" @on-change="setdate" style="width: 140px" 
                    ></DatePicker>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <Button type="primary" @click="sear_info">获取发票信息</Button>

            </p>
            <div>
                <Row :gutter="10" style="font-size:12px;"  class="r1">
                    <Col span="1" style="text-align:center;">购<br/>买<br/>方</Col>
                    <Col span="15" class="bor_left">
                        <p>名称：</p>
                        <input class="input1" v-model="obj.buyerName"  readonly="" ></br>
                        <p>纳税人识别号:</p>
                        <input class="input1" readonly=""  v-model="obj.buyerTaxNo" style="margin-left:0px;"></br>
                        <p>地 址、电 话:</p>
                        <input class="input1" v-model="obj.buyerAddressPhone" tabIndex="5" 
                        readonly="" ></br>
                        <p>开户行及帐号:</p>
                        <input class="input1" v-model="obj.buyerBankAccount" tabIndex="6" readonly="" >
                    </Col>
                    <Col span="1" class="bor_left" style="height:120px;text-align:center;">密<br/>码<br/>区</Col>
                    <Col span="7" class="bor_left" style="height:120px;"></Col>
                </Row>
                <Row :gutter="10" class="r2">
                    <Col span="5">
                        货物或应税劳务、服务名称</br>
                        <!-- @change="inputchange(item,index)" -->
                        <!-- <input type="text" v-for="(item,index) in obj.invoiceDetailList"
                         v-model="item.goodsName" :tabIndex=" 6+(index*7)+1" 
                         ref="goodsName"></br>
 -->

                         <span v-for="(item,index) in obj.invoiceDetailList">
                            
                            <i  class="ivu-icon ivu-icon-close"  @click="remove(item,index)"></i>
                            <input type="text"  style="width:90%;"
                            v-model="item.goodsName" :tabIndex=" 6+(index*7)+1" 
                            ref="goodsName"  @keyup.enter="enter(index,1)" >
                        </span>
                        <i 
                        style="font-size:16px;position: absolute;left: 0%;bottom: 50px;text-align:center;width:100%;cursor:pointer;color:#333;" 
                            @click="add()"
                        >
                            添加明细
                        </i>

                        <p class="p1">（明细最多100条）<br/>合计</p>
                    </Col>
                    <Col span="4" class="bor_left2">
                        规格型号</br>
                        <input v-for="(item,index) in obj.invoiceDetailList" v-model="item.goodsSpecification"  :tabIndex=" 6+(index*7)+2" 
                        @keyup.enter="enter(index,2)">
                        <p class="p1 _hide"></p>
                    </Col>
                    <Col span="2"  class="bor_left2">单位
                        <input v-for="(item,index) in obj.invoiceDetailList" v-model="item.goodsUnit" :tabIndex=" 6+(index*7)+3"  
                        @keyup.enter="enter(index,3)">
                        <p class="p1 _hide"></p>
                    </Col>
                    <Col span="2"  class="bor_left2">数量
                        <input onkeyup="value=value.replace(/[a-zA-Z]/g,'') "
                        v-for="(item,index) in obj.invoiceDetailList" v-model="item.goodsQuantity" @change="invnum(item)" ref="goodsQuantity" 
                        :tabIndex=" 6+(index*7)+4" 
                        @keyup.enter="enter(index,4)">
                        <p class="p1 _hide"></p>
                    </Col>
                    <Col span="3"  class="bor_left2">单价
                        <input onkeyup="value=value.replace(/[a-zA-Z]/g,'') "
                         v-for="(item,index) in obj.invoiceDetailList" v-model="item.goodsPrice"  @change="money(item)" ref="goodsPrice" 
                         :tabIndex=" 6+(index*7)+5"  style="text-align:right;" 
                         @keyup.enter="enter(index,5)">
                        <p class="p1 _hide"></p>
                    </Col>
                    <Col span="3"  class="bor_left2"  style="border-right:1px #e7922d solid;">金额
                        <input v-for="item in obj.invoiceDetailList" v-model="item.goodsTotalPrice" disabled=""  style="text-align:right;">
                        <p class="p1 _hide"></p>
                        <span class="input_cs">
                            <input type="text"  disabled :value="'￥'+obj.invoiceTotalPrice"  />
                        </span>
                    </Col>
                    <Col span="2"  class="bor_left2" style="border:0px;">税率<br/>
                         
                         <span  v-for="(item,index) in obj.invoiceDetailList">
                             <input onkeyup="value=value.replace(/[a-zA-Z]/g,'') "    v-model="item.goodsTaxRate"  style="width:50px;text-align:right;" ref="goodsTaxRate" :tabIndex=" 6+(index*7)+6" 
                             @change="settax(item,index)" 
                             @keyup.enter="enter(index,6)">%
                         </span>
                    </Col>
                    <Col span="3" class="bor_left2">税额
                        <input   style="text-align:right;" v-for="(item,index) in obj.invoiceDetailList" 
                        v-model="item.goodsTotalTax"   ref="goodsTotalTax" :tabIndex=" 6+(index*7)+7" disabled="">
                        <p class="p1 _hide"></p>
                        <span class="input_cs">
                            <input type="text"  disabled :value="'￥'+obj.invoiceTotalTax"  />
                        </span>
                    </Col>
                </Row>
                <Row :gutter="10" class="r3">

                    <Col span="5" style="text-align:center;">价税合计（ 大 写 ）</Col>
                    <Col span="19" class="bor_left3">


                    <input type="text"  disabled="" 
                    style="margin-right:20%;margin-left:2%;width:40%;" 
                    v-model="obj.invoiceTotalPriceb"> 
                    (小写)&nbsp;&nbsp;
                    <input type="text" disabled=""   :value="'￥'+obj.invoiceTotalPriceTax"></Col>
                </Row>
                <Row :gutter="10" class="r4">
                    <Col span="1" style="text-align:center;">销<br/>售<br/>方</Col>
                    <Col span="11" class="bor_left4">
                        <p>名称：</p><input class="input1" v-model="obj.sellerName" ref="sellerName" :tabIndex=" 6+(obj.invoiceDetailList.length*7)+1"></br>
                        <p>纳税人识别号:</p><input class="input1" v-model="obj.sellerTaxNo" 
                        ref="sellerTaxNo" :tabIndex=" 6+(obj.invoiceDetailList.length*7)+2"></br>
                        <p>地 址、电 话:</p><input class="input1" v-model="obj.sellerAddressPhone" :tabIndex=" 6+(obj.invoiceDetailList.length*7)+3"></br>
                        <p>开户行及帐号:</p>
                        <input class="input1" v-model="obj.sellerBankAccount" 
                        :tabIndex=" 6+(obj.invoiceDetailList.length*7)+4" style="margin-left:-2px;">
                    </Col>
                    <Col span="1" class="bor_left4"  style="text-align:center;">备<br/><br/>注</Col>
                    <Col span="11" class="bor_left4">校验码</Col>
                </Row>
                <p class="p_5">
                    <span>收 款 人 ：
                        <input class="_input1" v-model="obj.payee" :tabIndex=" 6+(obj.invoiceDetailList.length*7)+5"></span>
                    <span>复核 ：
                        <input class="_input1" v-model="obj.checker"  ref="checker" 
                        :tabIndex=" 6+(obj.invoiceDetailList.length*7)+6"></span>
                    <span>开票人 ：
                        <input class="_input1" v-model="obj.drawer" 
                        :tabIndex=" 6+(obj.invoiceDetailList.length*7)+7"></span>
                    <span>销售方 ： （章）</span>
                </p>
                
            </div>
        </div>

        <p slot="footer" >
            <Button @click="cancel()">关闭</Button>
            <Button type="primary" @click="submit('addForm')" >确认保存</Button>
        </p>
     </Modal>
</div>
</template>
<script>
 export default {
     data (){
         return {
             modal:false,title:'进项发票录入',date:'',
             obj:{mail:'',invoiceDetailList:[
                {goodsName:'',goodsTotalPrice:null}
             ],invoiceDate:''},
             ruleValidate:{
                 mail:[
                     { required: true, message: 'email不能为空', trigger: 'blur' },
                     {type:'email',message:'email 格式不正确',trigger:'blur'}
                 ]
             },c:{}
         }
     },methods:{
        remove(item,index){
            console.log(item,index);
            this.obj.invoiceDetailList.splice(index,1);
            this.setcou();
         },
         add(t){
            var i=this.obj.invoiceDetailList.length-1;
            if( i<0  || this.obj.invoiceDetailList[i].goodsName.length>0 ){
                this.obj.invoiceDetailList.push({goodsName:'',goodsTotalPrice:null});
                var a=(i+1)*7+7;
                console.log(a);
                setTimeout(()=>{
                    $('#div_box1').find('input[tabindex='+a+']').focus();
                })
                

                //this.showtitle(this.obj.invoiceDetailList[i+1],i+1)
            }else{
                 this.$Message.warning('请补齐空行数据');
            }
            

        },
        enter(index,num){
            var a=7+index*7;
            console.log(index,num,a);
            if(num<6){
                a+=num;
                console.log(a);
                $('#div_box1').find('input[tabindex='+a+']').focus();
            }else if(this.obj.invoiceDetailList.length-1==index){
                console.log('add')
                this.add();
                
            }else if(this.obj.invoiceDetailList.length-1>index){
                a+=num+1;
                console.log(a);
                $('#div_box1').find('input[tabindex='+a+']').focus();
            }

        },
        setdate(d){
            this.obj.invoiceDate=d;
        },
         showdata(params){
            this.modal=true;
            this.c={
                orderGuid:params,
                buyerName:this.$store.getters.getUserinfo.comName,
                buyerTaxNo:this.$store.getters.getUserinfo.taxNo
            }
            this.obj.orderGuid=params;
            this.obj.buyerName=this.$store.getters.getUserinfo.comName;
            this.obj.buyerTaxNo=this.$store.getters.getUserinfo.taxNo;
            this.obj.buyerAddressPhone=this.$store.getters.getUserinfo.addressPhone;
            this.obj.buyerBankAccount=this.$store.getters.getUserinfo.bankAccount;
            this.obj.invoiceTypeCode="1";
            this.obj.invoiceTotalTax=0;
            this.obj.invoiceTotalPrice=0;
            this.obj.invoiceTotalPriceTax=0;
            //this.obj.invoiceDate=null;
            //this.obj.date=null;
         },
         sear_info(){
            //037021700211  32936379 84878934052238537172
            //1100172320 30962578 85063023131549294954
            if(this.obj.invoiceCode==undefined || this.obj.invoiceCode==''){
                this.$Message.error('发票代码不能为空');
                this.$refs.invoiceCode.focus();
                return;
             }
             
             if(this.obj.invoiceNo==undefined || this.obj.invoiceNo==''){
                this.$Message.error('发票号不能为空');
                this.$refs.invoiceNo.focus();
                return;
             }
             if(this.obj.invoiceCheckCode==undefined || this.obj.invoiceCheckCode==''){
                this.$Message.error('校验码不能为空');
                this.$refs.invoiceCheckCode.focus();
                return;
             }
             if(this.obj.totalAmount==undefined || this.obj.totalAmount==''){
                this.$Message.error('总金额不能为空');
                this.$refs.totalAmount.focus();
                return;
             }
             if(this.obj.invoiceDate==undefined || this.obj.invoiceDate==''){
                this.$Message.error('开票日期不能为空');
                //this.$refs.invoiceDate.focus();
                return;
             }
             this.axios.post('/incomeinvoice/validateByNo',{
                invoiceNumber:this.obj.invoiceNo,
                invoiceCode:this.obj.invoiceCode,
                billingDate:this.obj.invoiceDate,
                totalAmount:this.obj.totalAmount,
                checkCode_6:this.obj.invoiceCheckCode
             }).then((res)=>{
                console.log(res);
                if(res.errorCode==0){
                    this.obj=res.data;
                    this.obj.orderGuid=this.c.orderGuid;
                    //this.obj.buyerName=this.c.buyerName;
                    this.obj.buyerTaxNo=this.c.buyerTaxNo;
                    this.obj.invoiceTypeCode="1"
                }
                
             })
         },
         submit(){
                
            
             
             if(this.obj.invoiceCode==undefined || this.obj.invoiceCode==''){
                this.$Message.error('发票代码不能为空');
                this.$refs.invoiceCode.focus();
                return;
             }
             if(this.obj.invoiceTypeCode==undefined || this.obj.invoiceTypeCode==''){
                this.$Message.error('请选择发票类型');
                return;
             }
             if(this.obj.invoiceNo==undefined || this.obj.invoiceNo==''){
                this.$Message.error('发票号不能为空');
                this.$refs.invoiceNo.focus();
                return;
             }
             if(this.obj.invoiceCheckCode==undefined || this.obj.invoiceCheckCode==''){
                this.$Message.error('校验码不能为空');
                this.$refs.invoiceCheckCode.focus();
                return;
             }
             if(this.obj.invoiceDate==undefined || this.obj.invoiceDate==''){
                this.$Message.error('开票日期不能为空');
                //this.$refs.invoiceDate.focus();
                return;
             }
             if(this.obj.invoiceDetailList.length>0){
                var t='';
                var u=this.obj.invoiceDetailList[0]
                //
                if(u.goodsName ==undefined || u.goodsName==''){
                    t+='服务名称,'
                    this.$refs.goodsName[0].focus()
                    
                }
                if(u.goodsQuantity ==undefined || u.goodsQuantity==''){
                    t+='数量,'
                    this.$refs.goodsQuantity[0].focus()
                    
                }
                if(u.goodsPrice ==undefined || u.goodsPrice==''){
                    t+='单价,'
                    this.$refs.goodsPrice[0].focus()
                }
                
                if(u.goodsTaxRate ==undefined || u.goodsTaxRate==''){
                    t+='税率,'
                    this.$refs.goodsTaxRate[0].focus()
                }
                if(u.goodsTotalTax ==undefined || u.goodsTotalTax==''){
                    t+='税额'
                    this.$refs.goodsTotalTax[0].focus()
                }
                //console.log(t);
                if(t!=''){
                    this.$Message.error(t+'不能为空');
                    return;
                }
             }
             if(this.obj.sellerName==undefined || this.obj.sellerName==''){
                this.$Message.error('销售方名称不能为空');
                this.$refs.sellerName.focus();
                return;
             }
             if(this.obj.sellerTaxNo==undefined || this.obj.sellerTaxNo==''){
                this.$Message.error('销售方纳税人识别号不能为空');
                this.$refs.sellerTaxNo.focus();
                return;
             }
             if(this.obj.drawer==undefined || this.obj.drawer==''){
                this.$Message.error('开票人不能为空');
                this.$refs.drawer.focus();
                return;
             }
             
             var url='/incomeinvoice/addInvoiceAndDetail';
             this.setcou();
             var c=Object.assign({},this.obj);
             if(c.invoiceDetailList[c.invoiceDetailList.length-1].goodsName!=''){

             }else{
                c.invoiceDetailList.pop();
             }
             //c.invoiceTotalTax=0;
             //c.invoiceTotalPrice=0;
             c.invoiceDetailList.forEach((r)=>{
                if(r.goodsTaxRate>0){
                    r.goodsTaxRate=r.goodsTaxRate/100;
                }
                //c.invoiceTotalTax+=Number(r.goodsTotalTax);
                //c.invoiceTotalPrice+=Number(r.goodsTotalPrice);
             })
             
             this.axios.post(url,c).then((res)=>{
                 console.log(res);
                 if(res.errorCode==0){
                    //成功
                     this.modal=false;
                     this.obj={invoiceDetailList:[
                        {goodsName:'',goodsTotalPrice:null}
                     ]};
                     this.obj.invoiceTypeCode="1";
                    this.obj.invoiceDate='';
                    this.obj.invoiceTotalTax=0;
                    this.obj.invoiceTotalPrice=0;
                    this.obj.invoiceTotalPriceTax=0;
                     this.date=null;
                     this.$Message.success('成功');
                     this.$emit('refresh');
                 }else{
                    //this.$message.error(res.message);
                 }
             })   
             return ;
             
            
         },
         cancel(){
             this.modal=false;
             
         },inputchange(item,index){
            if(item.goodsTotalTax!="" && index==this.obj.invoiceDetailList.length-1){
                this.obj.invoiceDetailList.push({
                    goodsName:'',goodsTotalPrice:null
                })
            }
         },money(item,index){
             if(item.goodsQuantity==null || item.goodsQuantity == undefined){
                 item.goodsQuantity=0;
             }
             if(item.goodsPrice==null || item.goodsPrice == undefined){
                 item.goodsPrice=null;
             }else{
                item.goodsPrice=Number(item.goodsPrice).toFixed(2);
             }
            
            item.goodsTotalPrice=((item.goodsQuantity || 0)*(item.goodsPrice || 0)).toFixed(2);

            

            // if(item.goodsTotalTax!=undefined && item.goodsTotalTax!=''){
            //     item.goodsTotalTax=Number(item.goodsTotalTax || 0).toFixed(2)
            // }else{
            //     item.goodsTotalTax=0;
            // }

            
            
            //console.log(item.goodsPrice,item.goodsTotalPrice,item.goodsTaxRate)
            if(item.goodsTotalPrice && item.goodsTaxRate){
                item.goodsTotalTax=(item.goodsTotalPrice*(item.goodsTaxRate/100)).toFixed(2)
                console.log(item.goodsTotalTax)
            }
            
            
            this.setcou();
            
            
         },settax(item,index){
             if(item.goodsTotalPrice && item.goodsTaxRate){
                var c=(item.goodsTotalPrice*(item.goodsTaxRate/100)).toFixed(2)
                console.log(c)
                item.goodsTotalTax=c;
                this.$set(this.obj.invoiceDetailList,index,item)
            }
            this.setcou();
            return ;
             if(index!=undefined){
                //console.log(index);
                this.inputchange(item,index);
            }
         },setcou(){
            var a=0;
            this.obj.invoiceTotalTax=0;
            this.obj.invoiceTotalPrice=0;
            this.obj.invoiceDetailList.forEach((i,index)=>{
                //console.log(i.goodsTotalPrice,Number(i.goodsTotalTax))
                if(i.goodsTotalPrice>0){
                    //console.log(index,i.goodsTotalPrice)
                    a+=Number(i.goodsTotalPrice) || 0;
                     this.obj.invoiceTotalPrice+=Number(i.goodsTotalPrice);
                }
                
                //console.log(a)
                
                if(i.goodsTotalTax>0){
                    console.log(index,i.goodsTotalTax)
                    a+=Number(i.goodsTotalTax) || 0;
                    this.obj.invoiceTotalTax+=Number(i.goodsTotalTax);
                    //console.log(this.obj.invoiceTotalTax)
                }
                
                //console.log(a)
            });
            a=Number(a).toFixed(2)
            //this.obj.invoiceTotalPrice=a;
            this.$set(this.obj,'invoiceTotalPriceTax',a)

            this.obj.invoiceTotalPriceb=changeMoneyToChinese(this.obj.invoiceTotalPriceTax);
        },invnum(item){
            item.goodsQuantity=parseInt(item.goodsQuantity) || 0;
            if(item.goodsPrice){
                item.goodsPrice=Number(item.goodsPrice).toFixed(2);
            }
            //if(item.goodsPrice && item.goodsQuantity){
                item.goodsTotalPrice=((item.goodsQuantity || 0)*(item.goodsPrice || 0)).toFixed(2)
            // this.setcou();
            this.money(item);
            //}
            

         }
     },created(){
         this.$bus.on('bus_entryedit', this.showdata);
         
     },beforeDestroy(){
         this.$bus.off('bus_entryedit',this.showdata)
     }
 }
 function changeMoneyToChinese(money){  
            var cnNums = new Array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖"); //汉字的数字  
            var cnIntRadice = new Array("","拾","佰","仟"); //基本单位  
            var cnIntUnits = new Array("","万","亿","兆"); //对应整数部分扩展单位  
            var cnDecUnits = new Array("角","分","毫","厘"); //对应小数部分单位  
            //var cnInteger = "整"; //整数金额时后面跟的字符  
            var cnIntLast = "元"; //整型完以后的单位  
            var maxNum = 999999999999999.9999; //最大处理的数字  
              
            var IntegerNum; //金额整数部分  
            var DecimalNum; //金额小数部分  
            var ChineseStr=""; //输出的中文金额字符串  
            var parts; //分离金额后用的数组，预定义  
            if( money == "" ){  
                return "";  
            }  
            money = parseFloat(money);  
            if( money >= maxNum ){  
                $.alert('超出最大处理数字');  
                return "";  
            }  
            if( money == 0 ){  
                //ChineseStr = cnNums[0]+cnIntLast+cnInteger;  
                ChineseStr = cnNums[0]+cnIntLast  
                //document.getElementById("show").value=ChineseStr;  
                return ChineseStr;  
            }  
            money = money.toString(); //转换为字符串  
            if( money.indexOf(".") == -1 ){  
                IntegerNum = money;  
                DecimalNum = '';  
            }else{  
                parts = money.split(".");  
                IntegerNum = parts[0];  
                DecimalNum = parts[1].substr(0,4);  
            }  
            if( parseInt(IntegerNum,10) > 0 ){//获取整型部分转换  
                var zeroCount = 0;  
                var IntLen = IntegerNum.length;  
                for( var i=0;i<IntLen;i++ ){  
                    var n = IntegerNum.substr(i,1);  
                   var  p = IntLen - i - 1;  
                   var  q = p / 4;  
                   var m = p % 4;  
                    if( n == "0" ){  
                        zeroCount++;  
                    }else{  
                        if( zeroCount > 0 ){  
                            ChineseStr += cnNums[0];  
                        }  
                        zeroCount = 0; //归零  
                        ChineseStr += cnNums[parseInt(n)]+cnIntRadice[m];  
                    }  
                    if( m==0 && zeroCount<4 ){  
                        ChineseStr += cnIntUnits[q];  
                    }  
                }  
                ChineseStr += cnIntLast;  
                //整型部分处理完毕  
            }  
            if( DecimalNum!= '' ){//小数部分  
                var decLen = DecimalNum.length;  
                for(var i=0; i<decLen; i++ ){  
                    n = DecimalNum.substr(i,1);  
                    if( n != '0' ){  
                        ChineseStr += cnNums[Number(n)]+cnDecUnits[i];  
                    }  
                }  
            }  
            if( ChineseStr == '' ){  
                //ChineseStr += cnNums[0]+cnIntLast+cnInteger;  
                ChineseStr += cnNums[0]+cnIntLast;  
            }/* else if( DecimalNum == '' ){ 
                ChineseStr += cnInteger; 
                ChineseStr += cnInteger; 
            } */  
            return ChineseStr;  
        }  
</script>
